package org.lds.message.center.email.autoconfigure;

import lombok.RequiredArgsConstructor;
import org.lds.message.center.email.EmailHandler;
import org.lds.message.center.email.EmailMessageService;
import org.lds.message.center.email.constant.EmailConstant;
import org.lds.message.center.email.consumer.EmailConsumer;
import org.lds.message.center.email.health.endpoint.EmailHealthEndpoint;
import org.lds.message.center.email.properties.EmailProperties;
import org.lds.message.center.email.starter.EmailRegisterAdmin;
import org.lds.message.center.plugin.core.kafka.KafkaProducer;
import org.lds.message.center.plugin.core.remote.RemoteService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;

@Configuration
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = EmailConstant.EMAIL_PREFIX, name = "enabled", havingValue = "true")
@EnableConfigurationProperties({EmailProperties.class})
public class EmailMessageAutoConfiguration {
    private final EmailProperties emailProperties;

    @Bean
    public EmailMessageService emailMessageService(KafkaProducer kafkaProducer, RemoteService remoteService, EmailHandler emailHandler) {
        return new EmailMessageService(emailHandler, emailProperties, kafkaProducer, remoteService);
    }

    @Bean
    public EmailHandler emailHandler(JavaMailSender mailSender) {
        return new EmailHandler(emailProperties, mailSender);
    }

    @Bean
    public EmailConsumer emailConsumer(EmailMessageService emailMessageService) {
        return new EmailConsumer(emailMessageService);
    }

    @Bean
    public EmailRegisterAdmin emailRegisterAdmin(RemoteService remoteService) {
        return new EmailRegisterAdmin(remoteService);
    }

    @Bean
    public EmailHealthEndpoint emailHealthEndpoint() {
        return new EmailHealthEndpoint();
    }

}
